using System;
using System.Collections;
using System.Collections.Generic;
using Main.Details;
using Main.Event;
using Main.GameTime;
using UnityEngine;
using ZSW.Framework;

namespace Main.CropPlant
{
    public class CropManager : ZSWF_Singleton<CropManager>
    {
        public CropDetailsList_SO cropData;
        private Transform CropParent;
        private Grid currentGrid;

        public override void Init()
        {
            base.Init();
            MainEventSystem.PlantSeedEvent += OnPlanSeed;
            MainEventSystem.AfterLoadSceneEvent += OnAfterLoadScene;
        }

        private void OnDisable()
        {
            MainEventSystem.PlantSeedEvent -= OnPlanSeed;
        }

        private void OnAfterLoadScene()
        {
            currentGrid = FindObjectOfType<Grid>();
            CropParent = GameObject.FindWithTag(Settings.CropParent).transform;
        }

        private void OnPlanSeed(int seedID, TileDetails tileDetails)
        {
            // 检索种子数据库,获取对应的种子信息
            // 验证是否为当前季节
            // 验证土地是否有其他种子
            if (TryGetCropDetails(seedID, out var cropDetails))
            {
                if (isSeasonValid(cropDetails) && tileDetails.seedItemID == -1)
                {
                    tileDetails.seedItemID = seedID;
                    tileDetails.growDays = 0;
                }
                // 刷新种子图片
                else if (tileDetails.seedItemID != -1)
                {

                }
                DisplayCropPlant(tileDetails, cropDetails);
            }
        }

        private void DisplayCropPlant(TileDetails tileDetails, CropDetails cropDetails)
        {
            int growthStage = cropDetails.growDays.Length;
            int currentStage = 0;
            int dayCounter = cropDetails.DayAmount;

            for (int i = growthStage - 1; i > -0; i--)
            {
                // 成熟判断
                if (dayCounter <= tileDetails.growDays)
                {
                    currentStage = i;
                    break;
                }
                dayCounter -= cropDetails.growDays[i];
            }

            //  根据当前阶段
            GameObject cropPrefab = cropDetails.growPrefabs[currentStage];
            Sprite cropSprite = cropDetails.growSprites[currentStage];

            // grid坐标为网格左下角, 0.5f为网格中心
            Vector3 pos = new Vector3(tileDetails.girdX + 0.5f, tileDetails.girdY + 0.5f, 0);

            GameObject cropInstance = Instantiate(cropPrefab, pos, Quaternion.identity, CropParent);

            cropInstance.GetComponentInChildren<SpriteRenderer>().sprite = cropSprite;
            cropInstance.GetComponent<Crop>().cropDetails = cropDetails;
            cropInstance.GetComponent<Crop>().tileDetails = tileDetails;

        }

        public bool TryGetCropDetails(int seedID, out CropDetails cropDetails)
        {
            cropDetails = cropData.cropDetails.Find(x => x.seedItemID == seedID);
            return cropDetails != null;
        }

        /// <summary>
        /// 校验种植季节是否合法
        /// </summary>
        /// <param name="cropDetails"></param>
        /// <returns></returns>
        private bool isSeasonValid(CropDetails cropDetails)
        {
            if (cropDetails == null)
            {
                return false;
            }

            foreach (var season in cropDetails.seasons)
            {
                if (season == TimeManager.Season)
                {
                    return true;
                }
            }
            return false;
        }

    }
}